package com.eascs.web.o2o.web.controller;

import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.o2o.web.entity.WfMessage;
import com.eascs.web.o2o.web.form.SmsForm;
import com.eascs.web.o2o.web.service.IMessageService;

@RestController
@RequestMapping("/restsms")
public class SmsRestController extends BaseRestController{
	
	@Autowired
	IMessageService messageService;
	
	@RequestMapping("/sms")
	public PaginationRestResult getSms(SmsForm smsForm){
		String o2ourl = "/restsms/sms";
		if(smsForm != null){
			String requestId = smsForm.getRequestId() == null ? "" : smsForm.getRequestId();
			String systemCode = smsForm.getSystemCode() == null ? "" : smsForm.getSystemCode();
			String receiveTelphone = smsForm.getReceiveTelphone() == null ? "" : smsForm.getReceiveTelphone();
			String startTime = smsForm.getStartTime() == null ? null : smsForm.getStartTime();
			String endTime = smsForm.getEndTime() == null ? null : smsForm.getEndTime();
			String status = smsForm.getStatus() == null ? "" : smsForm.getStatus();
			String content = smsForm.getContent() == null ? "" : smsForm.getContent();
			
			o2ourl = UrlUtil.appendUrl(o2ourl, "requestId", requestId);
			o2ourl = UrlUtil.appendUrl(o2ourl, "systemCode", systemCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "receiveTelphone", receiveTelphone);
			o2ourl = UrlUtil.appendUrl(o2ourl, "startTime", startTime);
			o2ourl = UrlUtil.appendUrl(o2ourl, "endTime", endTime);
			o2ourl = UrlUtil.appendUrl(o2ourl, "status", status);
			o2ourl = UrlUtil.appendUrl(o2ourl, "content", content);
			o2ourl = o2ourl + "&index=";
		}else{
			o2ourl = o2ourl + "?index=";
		}
		smsForm.setPageSize(10);
		PageResult<Object[]> result = messageService.getSmsInfo(smsForm);
		List<Object[]> date = result.getData();
		String titles[] = {"请求系统流水号","系统编码","业务编码","发送类型","接收号码","创建时间","发送状态"};
		Button detail = ButtonHelper.createAjaxButton(null, "/restsms/detail?requestId=");
		Button[]  titleButtons= new Button[] { detail};
		SelectDataOption[] selectDataOptions = {null,null,null,null,null,null,null};
		PaginationInfo info = new PaginationInfo(result, titles, o2ourl);
		info.setFirstRenderColumnIndex(0);
		info.setFormats(new Format[] {null,null,null,null,null,null,null});
		info.setRendTo("#o2o_result");
		info.setSelectDataOptions(selectDataOptions);
		info.setBuildPagingNavigation(true);
		info.setTitleButtons(titleButtons);
		ViewContext.put("vo", smsForm);
		return PaginationRestResult.buildPaginationRestResult(info);
	}
	
	@RequestMapping(value="/detail")
	public BaseRestResult<String> detail(String requestId){
		WfMessage wf = messageService.findByrequestId(requestId);
		ViewContext.put("wf", wf);
		return HtmlRestResult.popHtmlFragment("sms/smsList_detail");
	}
}
